var utils = require("../utils/utils");
var ocr_utils = require("../utils/ocr_utils");
var in_task = require("./inTask");
var point_map = require("./pointMap");
//随机搜 30次
var search_30 = {
  is_over_today: false,
  has_finished_times: 0,
  go_page_from_home() {
    var logger = utils.create_logger("search_30 go_page_from_home: ");
    if (ocr_utils.screen_exists_text_all(["首页", "视频"])) {
      logger("");
      utils.click_point.apply(
        utils,
        utils.get_size_point(point_map["首页任务"], 1080, 2340)
      );
      sleep(utils.get_random_interval(2, 0, 2, 1000));
      if (this.is_over_today) {
        logger("今天的--search30--任务全部完成了");
        this.next();
        return;
      }
      this.init();
    } else {
      logger("NOT FOUND");
    }
  },
  init() {
    var logger = utils.create_logger("search_30 init: ");
    sleep(utils.get_random_interval(3, 0, 2, 1000));
    in_task.init();
    var ocr_text = ocr_utils.find_text(
      "随机搜赚钱",
      false,
      null,
      null,
      (device.height / 10) * 3
    );
    if (ocr_text) {
      var x = ocr_text.bounds.centerX();
      var y = ocr_text.bounds.centerY();
      logger("找到【随机搜赚钱】", "坐标：x: " + x + " y: " + y);
      this.run(x, y);
    } else {
      logger("没找到【随机搜赚钱】，所以判断今天的随机搜赚钱已经全部结束");
      this.is_over_today = true;
      this.next();
    }
  },
  run(x, y) {
    var logger = utils.create_logger("search_30 run: ");
    for (var index = 0; index < 2; index++) {
      utils.click_x_y(x, y);
      sleep(utils.get_random_interval(2, 0, 2, 1000));
      var search_swipe_times = 5 + random(0, 2);
      logger(
        "即将开始第--" +
          (index + 1) +
          "--次搜索，需要滑动--" +
          search_swipe_times +
          "--次"
      );
      var up_times = 0;
      var down_times = 0;
      var max_up_times = utils.get_random_interval(2, 0, 1, 1);
      logger("最多向上滑动-" + max_up_times + "-次");
      while (search_swipe_times > 0) {
        if (up_times < max_up_times) {
          utils.swipe_x(
            device.width / 2,
            (device.height / 10) * 5,
            device.width / 2,
            (device.height / 10) * 2,
            utils.get_random_interval(300, 0, 700, 1)
          );
          up_times = up_times + 1;
          logger("已经向上滑动-1-次");
        } else {
          var random_num = random(0, 1);
          if (random_num == 0 && down_times < up_times) {
            //向下滑动
            utils.swipe_x(
              device.width / 2,
              (device.height / 10) * 5,
              device.width / 2,
              (device.height / 10) * 8,
              utils.get_random_interval(300, 0, 700, 1)
            );
            down_times = down_times - 1;
            up_times = up_times + 1;
            logger("已经向上滑动-1-次");
            down_times = down_times + 1;
            up_times = up_times - 1;
            logger("已经向下滑动-1-次");
          } else {
            utils.swipe_x(
              device.width / 2,
              (device.height / 10) * 8,
              device.width / 2,
              (device.height / 10) * 5,
              utils.get_random_interval(300, 0, 700, 1)
            );
            down_times = down_times - 1;
            up_times = up_times + 1;
            logger("已经向上滑动-1-次");
          }
        }

        var watch_search_result_time = 3 + random(0, 2);
        sleep(watch_search_result_time * 1000);
        search_swipe_times = search_swipe_times - 1;
        if (search_swipe_times == 0) {
          logger("第" + (index + 1) + "次搜索完成");
        } else {
          logger("还需要滑动" + search_swipe_times + "次");
        }
      }
      back();
      sleep(utils.get_random_interval(2, 0, 2, 1000));
      if (utils.click_text_starts_with("残忍离开")) {
        logger("找到--残忍离开--");
        sleep(utils.get_random_interval(2, 0, 2, 1000));
      } else {
        logger("没找到--残忍离开--");
      }
      this.has_finished_times = this.has_finished_times + 1;
      if (this.has_finished_times == 30) {
        logger(
          "今天已经搜索--" +
            this.has_finished_times +
            "--次，今天的搜索30次任务已经全部完成"
        );
        this.is_over_today = true;
      } else {
        logger("今天已经搜索--" + this.has_finished_times + "--次");
      }
      in_task.init();
      sleep(utils.get_random_interval(2, 0, 2, 1000));
    }
    this.after();
  },
  after() {
    console.log("search_30 after: ");
  },
  next() {
    console.log("search_30 next: ");
    console.hide();
  },
};
module.exports = search_30;
